package com.controller;

import com.model.auto.AiQuestion;
import com.model.auto.vo.AiMessage;
import com.model.auto.vo.AiMessageRequest;
import com.model.auto.vo.AiMessageResponse;
import com.service.IBaiduAiService;
import com.util.R;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import reactor.core.publisher.Flux;

import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

/**
 * AI问答
 * @author zhaoyifan
 * @date 2023/12/13 17:11
 */
@Controller
@RequestMapping("/baidu/ai")
@Slf4j
public class BaiduAiController extends BaseController{

    @Autowired
    IBaiduAiService baiduAiService;

    /**
     * AI问答提交问题
     * @param aiMessageList
     * @return
     */
    @PostMapping("/call")
    @ResponseBody
    public R call(@RequestBody List<AiMessage> aiMessageList) {
        try {
            String content = baiduAiService.callAi(aiMessageList);
            return R.ok(content);
        }catch (Exception e){
            return R.error("请求异常，请刷新重试");
        }
    }

    /**
     * AI问答提交问题(流式)
     * @param aiMessageList
     * @return
     */
    @PostMapping(value = "/callStream", produces = MediaType.TEXT_EVENT_STREAM_VALUE)
    public Flux<String> callStream(@RequestBody List<AiMessage> aiMessageList) {
        try {
            return baiduAiService.callStream(aiMessageList)
                    .map(response -> response.getResult());
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }
    /**
     * AI问答提交问题(流式)
     * @param aiMessageList
     * @return
     */
    @GetMapping(value = "/callStreamTest", produces = MediaType.TEXT_EVENT_STREAM_VALUE)
    public Flux<String> callStream() {
        try {
            List<AiMessage> aiMessageList = new ArrayList<>();
            AiMessage aiMessage = new AiMessage();
            aiMessage.setRole("user");
            aiMessage.setContent("清华大学介绍");
            aiMessageList.add(aiMessage);
            return baiduAiService.callStream(aiMessageList)
                    .map(response -> response.getResult());
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }

    /**
     * AI问答提交问题V2
     * @param request
     * @return
     */
    @PostMapping("/callV2")
    @ResponseBody
    public R callV2(@RequestBody AiMessageRequest messageRequest, HttpServletRequest request) {
        Long userId = getUserId(request);
        try {
            AiMessageResponse response = baiduAiService.callAiV2(messageRequest, userId);
            return R.ok(response);
        }catch (Exception e){
            log.error("请求异常，异常信息=", e);
            return R.error("请求异常，请刷新重试");
        }
    }

    /**
     * 获取对话记录
     * @return
     */
    @GetMapping("/chatList")
    @ResponseBody
    public R chatList(HttpServletRequest request) {
        Long userId = getUserId(request);
        List<AiQuestion> list = baiduAiService.chatList(userId);
        return R.ok(list);
    }

    /**
     * 更新标题
     * @return
     */
    @PostMapping("/updateChatTitle")
    @ResponseBody
    public R updateChatTitle(@RequestBody AiQuestion aiQuestion) {
        baiduAiService.updateChatTitle(aiQuestion);
        return R.ok();
    }

    /**
     * 删除对话
     * @return
     */
    @PostMapping("/deleteChat")
    @ResponseBody
    public R deleteChat(@RequestBody AiQuestion aiQuestion) {
        baiduAiService.deleteChat(aiQuestion);
        return R.ok();
    }
}
